package com.example.backend.controller;

import com.example.backend.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/admin")
public class AdminController {
    /**
     * 审核
     * 登录《、审核资质《，修改密码《
     */
    final static int accountType = 4;
    
    @Autowired
    private AdminService adminService;

    //登录
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> credentials, HttpServletResponse response) {
        String accountId = credentials.get("accountId");
        String password = credentials.get("password");

        Map<String, Object> resp = new HashMap<>();

        // 验证必要参数
        if (accountId == null || password == null) {
            resp.put("message", "输入不能为空");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
        }

        // 调用登录服务
        String result = adminService.login(accountId, password, accountType);
        
        if ("登录成功".equals(result)) {
            // 登录成功，设置Cookie
            Cookie accountCookie = new Cookie("accountId", accountId);
            accountCookie.setMaxAge(24 * 60 * 60); // 24小时
            accountCookie.setPath("/");
            response.addCookie(accountCookie);

            resp.put("message", result);
            resp.put("accountId", accountId);
            return ResponseEntity.ok(resp);
        } else {
            // 登录失败
            resp.put("message", result);
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(resp);
        }
    }

    //修改密码
    @PostMapping("/changePassword")
    public ResponseEntity<Map<String, Object>> changePassword(@RequestBody Map<String, String> credentials) {
        String accountId = credentials.get("accountId");
        String oldPassword = credentials.get("oldPassword");
        String newPassword = credentials.get("newPassword");

        Map<String, Object> resp = new HashMap<>();

        // 验证必要参数
        if (accountId == null || oldPassword == null || newPassword == null) {
            resp.put("message", "accountId, oldPassword and newPassword are required");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
        }

        // 调用修改密码服务
        String result = adminService.changePassword(accountId, oldPassword, newPassword,accountType);

        resp.put("message", result);
        if ("修改成功".equals(result)) {
            return ResponseEntity.ok(resp);
        } else {
            // 修改失败
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
                    .body(resp);
        }
    }

    //获取所有用户
    @PostMapping("/getAllUsers")
    public ResponseEntity<Map<String, Object>> getAllUsers() {
        Map<String, Object> resp = new HashMap<>();
        Object users = adminService.getAllUsers();
        resp.put("message", "查询成功");
        resp.put("data", users);
        return ResponseEntity.ok(resp);
    }

    //审核资质
    @PostMapping("/auditQualification")
    public ResponseEntity<Map<String, Object>> auditQualification(@RequestBody Map<String, String> credentials) {
        String accountId = credentials.get("accountId");
        Map<String, Object> resp = new HashMap<>();
        if (accountId == null){
            resp.put("message", "请正确选择");
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resp);
        }
        String result = adminService.auditQualification(accountId);
        resp.put("message", result);
        if ("审核成功".equals(result)){
            return ResponseEntity.ok(resp);
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(resp);
    }
}